package com.baizhi.controller;


import com.baizhi.entity.AttrAttrgroupRelation;
import com.baizhi.entity.AttrGroupDto;
import com.baizhi.service.IAttrAttrgroupRelationService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 属性&属性分组关联 前端控制器
 * </p>
 *
 * @author haiyu
 * @since 2021-09-16
 */
@RestController
@RequestMapping("/attrAttrgroupRelation")
public class AttrAttrgroupRelationController {

    @Autowired
    private IAttrAttrgroupRelationService iat;

    @PostMapping("/add")
    public void selectAdd(@RequestBody AttrGroupDto dto) {
        //先移除老数据，在保存新数据
        QueryWrapper<AttrAttrgroupRelation> wrapper = new QueryWrapper<>();
        wrapper.eq("attr_group_id",dto.getGroupId());
        iat.remove(wrapper);
        //下面是保存新数据
        String[] attrIds = dto.getAttrIds();
        for (String attrId:attrIds){
            AttrAttrgroupRelation aar = new AttrAttrgroupRelation();
            aar.setAttrGroupId(dto.getGroupId());
            aar.setAttrId(Long.parseLong(attrId));//String类型转为Long类型
            iat.save(aar);
        }
    }
    //根据分组的id查询出该分组下面的所有的atteids
    @GetMapping("/selectBIds/{groupId}")
    public List<AttrAttrgroupRelation> selectByIds(@PathVariable("groupId") Integer groupId){
        QueryWrapper<AttrAttrgroupRelation> wrapper = new QueryWrapper<>();
    wrapper.eq("attr_group_id",groupId);
        List<AttrAttrgroupRelation> list = iat.list(wrapper);
        return list;
    }

}
